<?php
use yii\helpers\Html;
use yii\helpers\Url;
use backend\widgets\GridView;
use yii\widgets\Breadcrumbs;
use yii\bootstrap\Modal;
$this->title = '标题列表';
Modal::begin([
    'id' => 'common-modal',
    'header' => '<h4 class="modal-title"></h4>',
    'footer' => '<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>',
]);


$js = <<<JS
$(".modaldialog").click(function(){ 
        aUrl = $(this).attr('data-url');
        aTitle = $(this).attr('data-title');
        $($(this).attr('data-target')+" .modal-title").text(aTitle);
        $($(this).attr('data-target')).modal("show")
             .find(".modal-body")
             .load(aUrl); 
        return false;
   }); 
JS;
$this->registerJs($js);

Modal::end(); 
?>

<div class="RightCenter clear">
    <div class="RightTime">
        <div class="Time">
            <h3>标题列表</h3>
        </div>
    </div>
    <div class="Right_common" style="height:90px;">
            <p><?php echo Html::a('新建标题', 'create', [
                    'id' => 'create',
                    'class' => 'modaldialog btn btn-success',
                    'data-toggle' => 'modal',
                    'data-url' => Url::toRoute(['create']),
                    'data-title' => '新建标题',
                    'data-target' => '#common-modal',
                ]);?>
            </p>
            <p><?= Html::a("批量删除", "javascript:void(0);", ["class" => "btn btn-success gridview"]) ?></p>
    </div>
<!--    <div class="Read_date" style="margin-top:20px;margin-bottom: 5px;height:60px;">
        <ul>
            <li class="Read_LisJ" style="margin-top:12.5px;">
                <div class="Seek">
                    <input type="text" class="form-control AppIN" id="exampleInputAmount" placeholder="搜索标题内容" value="<?php // echo isset($search['name']) && trim($search['name']) ? trim($search['name']) : '';?>">
                    <button type="button" class="btn btn-primary SS">搜索</button>
                </div>
                <div class="SX clear" >
                    筛选:
                    <select id="status">
                        <option <?php // echo $search['sort'] == 0 ? 'selected="selected"' : '';?> value="0">升序</option>
                        <option <?php // echo $search['sort'] == 1 ? 'selected="selected"' : '';?> value="1">降序</option>
                    </select>
                    <button class="btn btn-primary czS" type="button">重置</button>
                </div>
            </li>
        </ul>
    </div>-->
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'id' => 'grid',
        'columns' => [
            [
                'class' => 'yii\grid\CheckboxColumn',
                'headerOptions' => ['width' => 35],
                'checkboxOptions' => function($model, $key, $index, $column) {
                    if (isset($_COOKIE['id'])) return ['checked'=>in_array($model->id, $_COOKIE['id']) ? 'checked' : ''];
                }
            ],
            [
                'attribute' => 'id',
                'format' => 'raw',
                'contentOptions' => ['class' => 'TR_Tows'],
                'label' => '标题编号',
                'headerOptions' => ['width' => 10],
            ],
            [
                'attribute' => 'name',
                'label' => '标题名称',
                'headerOptions' => ['width' => 30],
            ],
            [
                'attribute' => 'create_time',
                'label' => '创建时间',
                'headerOptions' => ['width' => 80],
            ],
            [
                'class' => \yii\grid\ActionColumn::className(),
                'header' => '操作',
                'headerOptions'=>['width' => 180],
                'contentOptions'=>['class'=>'action-link'],
                'template' => '{update} {del}',
                'buttons' => [
                    
                   'update' => function ($url, $model, $key) {
                        return Html::a('编辑', $url, [
                             'id' => 'uodate-title',
                            'class' => 'modaldialog',
                            'data-toggle' => 'modal',
                            'data-url' => $url,
                            'data-title' => '编辑',
                            'data-target' => '#common-modal',
                        ])." ";
                    },
                   'del' => function ($url, $model, $key) {
                        return Html::a('删除', $url, [
                            'data' => [
                                'confirm' => '确定/删除？',
                                'method' => 'post',
                            ],
                        ]);
                    },
                    
                ],
            ],
        ],
    ]); ?>
</div>
<div class="button_delTow">
    <h4>提示</h4>
    <p class="modal_onep">确定删除选中标题？删除后不可逆</p>
    <button class="modal_bots">删除</button>
    <button class="modal_botq">取消</button>
</div>
<script>
    $(".gridview").on("click", function () {
        $(".button_delTow").show()
    });
     $(".modal_bots").click(function(){
        var keys = $("#grid").yiiGridView("getSelectedRows");
        console.log(keys);
        $.post( '/title/del-titles',{ids:keys.join(',')},function(result){
             alert(result)
             location.reload()
        });
    });
    $(".modal_botq").click(function () {
        $(".button_delTow").hide()
        location.reload()
    })
    
    $('.SS').click(function(){
        var name = $("#exampleInputAmount").val();
        var sort = $("#status").val();
        location.href="/title/index?name="+name+"&sort="+sort
    });
    
    $(".czS").click(function(){
        location.href="/title/index"
    });
</script>